/*
** ADOBE SYSTEMS INCORPORATED
** Copyright 2012 Adobe Systems Incorporated
** All Rights Reserved.
**
** NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the
** terms of the Adobe license agreement accompanying it.  If you have received this file from a
** source other than Adobe, then your use, modification, or distribution of it requires the prior
** written permission of Adobe.
*/
package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display3D.*;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	import starling.core.Starling;
	import starling.core.RenderSupport;
	import starling.display.DisplayObject;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.textures.Texture;

	import sample.lua.CModule;
	import sample.lua.__lua_objrefs;
	import Lua;
	
	public class LuaDisplayObject extends DisplayObject
	{
		private var currentGame:LuaGame
		private var renderFunc:String

		public function LuaDisplayObject(currentGame:LuaGame, renderFunc:String)
		{
			trace("LuaDisplayObject ctor");
			super()
			touchable = false
			this.currentGame = currentGame
			this.renderFunc = renderFunc
		}

		public override function getBounds(targetSpace:DisplayObject, resultRect:Rectangle=null):Rectangle {
			return null;
		}

		private function push_objref(o:*):void
		{
			var udptr:int = Lua.push_flashref(currentGame.luastate)
			sample.lua.__lua_objrefs[udptr] = o
		}

		public override function render(support:RenderSupport, alpha:Number):void {
			try {
				Lua.lua_getglobal(currentGame.luastate, renderFunc)
				push_objref(support)
				Lua.lua_callk(currentGame.luastate, 1, 0, 0, null)
			} catch(e:*) {
				currentGame.onError("Error during LuaDisplayObjectGame.render (luastate: "+currentGame.luastate+", func: "+renderFunc+"): " + e.toString())
			}
		}
	}
}
